package com.atguigu.daijia.common.login;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.common.util.AuthContextHolder;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.lang.reflect.Method;

/*
@description:
@ClassName LoginAspect
@author chen
@create 2024-08-01 15:08
@Version 1.0
*/
@Component
@Aspect  //切面类
public class LoginAspect {
    @Resource
    private RedisTemplate redisTemplate;


    // 定义切点
    @Pointcut("@annotation(com.atguigu.daijia.common.login.Login)")
    public void methodPointCut() {

    }


    /*
     * @param point 
     * @==============History===============<br/>;
     * @Description //   配置前置通知
     * @Date 2024/8/1
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    @Before("methodPointCut()")
    public void login(JoinPoint point)  throws Throwable {
        // 获取注解
        Login annotationLog = getAnnotationLog(point);
        if (annotationLog == null) {
            return;
        }
        //1 获取request对象
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        assert servletRequestAttributes != null;
        HttpServletRequest request = servletRequestAttributes.getRequest();

        //2 从请求头获取token
        String token = request.getHeader("token");

        //3 判断token是否为空，如果为空，返回登录提示
        if(!StringUtils.hasText(token)) {
            throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
        }

        //4 token不为空，查询redis
        String customerId = (String)redisTemplate.opsForValue()
                          .get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);

        //5 将redis中的用户id解析出来,存入ThreadLocal
        if(StringUtils.hasText(customerId)) {
            AuthContextHolder.setUserId(Long.parseLong(customerId));
        }
    }


    /*
     * @param joinPoint
     * @==============History===============<br/>;
     * @Description //   获取注解
     * @Date 2024/8/1
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    private Login getAnnotationLog(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();

        if (method != null) {
            return method.getAnnotation(Login.class);
        }
        return null;
    }


}
